package org.base;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Root;

@Named(value = "paseController")
@ViewScoped
public class PaseController extends AbstractController<Pase> {

    @Inject
    private ExpedienteController expedienteController;
    @Inject
    private DepartamentoController departamentoOrigenController;
    @Inject
    private DepartamentoController departamentoDestinoController;
    
    @Inject
    private AbstractFacade<Pase> ejbFacade;
    
    private List<Pase> pasesAsociados = new ArrayList<>();
    
    public PaseController() {
        // Inform the Abstract parent controller of the concrete Pase?cap_first Entity
        super(Pase.class);
        
        
    }

    /**
     * Resets the "selected" attribute of any parent Entity controllers.
     */
    public void resetParents() {
        setSelected(null);
        expedienteController.setSelected(null);
        departamentoOrigenController.setSelected(null);
        departamentoDestinoController.setSelected(null);
    }

    /**
     * Sets the "selected" attribute of the Expediente controller in order to
     * display its data in a dialog. This is reusing existing the existing View
     * dialog.
     *
     * @param event Event object for the widget that triggered an action
     */
    public void prepareExpediente(ActionEvent event) {
        if (this.getSelected() != null && expedienteController.getSelected() == null) {
            expedienteController.setSelected(this.getSelected().getExpediente());
            pasesAsociados = super.pasesAsociados(this.getSelected().getExpediente());
        }
    }

    /**
     * Sets the "selected" attribute of the Departamento controller in order to
     * display its data in a dialog. This is reusing existing the existing View
     * dialog.
     *
     * @param event Event object for the widget that triggered an action
     */
    public void prepareDepartamentoOrigen(ActionEvent event) {
        if (this.getSelected() != null && departamentoOrigenController.getSelected() == null) {
            departamentoOrigenController.setSelected(this.getSelected().getDepartamentoOrigen());
        }
    }

    
    /**
     * Sets the "selected" attribute of the Departamento controller in order to
     * display its data in a dialog. This is reusing existing the existing View
     * dialog.
     *
     * @param event Event object for the widget that triggered an action
     */
    public void prepareDepartamentoDestino(ActionEvent event) {
        if (this.getSelected() != null && departamentoDestinoController.getSelected() == null) {
            departamentoDestinoController.setSelected(this.getSelected().getDepartamentoDestino());
        }
    }
    
    public void miAction(Expediente exp){
        
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext ctxt = facesContext.getExternalContext();  
        try {
            ctxt.redirect("/sigei/faces/pase/index.xhtml");
//            expedienteController.setSelected(exp);
//            pasesAsociados = super.pasesAsociados(exp);
        } catch (IOException ex) {
            Logger.getLogger(PaseController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public List<Pase> pases(Expediente exp){
        return super.pasesAsociados(exp);
    }
    
    public void nuevoPase(ActionEvent event){
        
        Expediente exp = (Expediente) event.getComponent().getAttributes().get("expediente");
        
        expedienteController.setSelected(exp);
        expedienteController.setPasesAsociados(super.pasesAsociados(exp));
        pasesAsociados = super.pasesAsociados(exp);

//        Pase pase = new Pase();
//        pase.setId(99999L);
//        pase.setExpediente(exp);
//        setSelected(pase);
        
    }

    
//    public List<Pase> pasesAsociados(Expediente exp){
//    List<Pase> pases = new ArrayList<>();
//    
//    if (exp != null){
//        CriteriaBuilder cBuilder = ejbFacade.getEntityManager().getCriteriaBuilder();
//        javax.persistence.criteria.CriteriaQuery cq = cBuilder.createQuery();
//        
//        Root<Pase> c = cq.from(Pase.class);
//        cq.select(c);
//        
//        cq.where(cBuilder.equal(c.get("expediente"), exp));
//        
//        pases = ejbFacade.getEntityManager().createQuery(cq).getResultList();
//        }
//    
//    return pases;
//    
//    }

    public List<Pase> getPasesAsociados() {
        return pasesAsociados;
    }

    public void setPasesAsociados(List<Pase> pasesAsociados) {
        this.pasesAsociados = pasesAsociados;
    }

    public ExpedienteController getExpedienteController() {
        return expedienteController;
    }

    public void setExpedienteController(ExpedienteController expedienteController) {
        this.expedienteController = expedienteController;
    }
    
    public void init(){
        System.out.println("Expediente id: " + expedienteController.getSelected().getId());
    }
    
}
